package com.echat.serviceapigateway.security.model.authentication;

import com.echat.commonlib.constant.ErrorCode;
import com.echat.commonlib.exception.exc.ValidateFailedException;
import com.echat.serviceapigateway.security.model.BaseModel;
import com.echat.serviceapigateway.security.tool.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.time.LocalDateTime;


/**
 * Created by chenmohan on 2018/08/30
 *
 * @description
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@NameStyle(Style.camelhumpAndLowercase)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User extends BaseModel {

    private Long id;

    @Id
    private String userName;

    private String accessKey;

    private String accessSecret;

    private String signToken;

    private String redirectUri;

    private Integer status;

    private String memo;

    @Transient
    @JsonIgnore
    private String ipWhilteList;

    @JsonFormat(pattern="yyyy年MM月dd日HH时mm分")
    @Column(name = "create_datetime")
    protected LocalDateTime createTime;

    @JsonFormat(pattern="yyyy年MM月dd日HH时mm分")
    @Column(name = "modify_datetime")
    protected LocalDateTime modifyTime;

    public void checkIdNotNull() {
        if (this.id == null) {
            throw new ValidateFailedException(ErrorCode.User.USERNAME_NOT_NULL, "field[id] must not be null");
        }
    }

    public void checkUserNameNotNull() {
        if (StringUtils.isEmpty(this.userName)) {
            throw new ValidateFailedException(ErrorCode.User.USERNAME_NOT_NULL, "field[userName] must not be null or empty");
        }
    }

    public void checkAccessKeyNotNull() {
        if (StringUtils.isEmpty(this.accessKey)) {
            throw new ValidateFailedException(ErrorCode.User.USERNAME_NOT_NULL, "field[accessKey] must not be null or empty");
        }
    }

    public void checkAccessSecrectNotNull() {
        if (StringUtils.isEmpty(this.accessSecret)) {
            throw new ValidateFailedException(ErrorCode.User.USERNAME_NOT_NULL, "field[accessSecrect] must not be null or empty");
        }
    }
}
